/**
 * StarBannerBillboard.java
 *
 * Roslan's Remake of MOO in 3D using Ardor3D
 * (C) 2011 Roslan Amir. All rights reserved.
 */

package com.roslan.games.moo3d.spatial;

import com.ardor3d.image.Texture;
import com.ardor3d.image.TextureStoreFormat;
import com.ardor3d.math.ColorRGBA;
import com.ardor3d.renderer.state.MaterialState;
import com.ardor3d.renderer.state.RenderState.StateType;
import com.ardor3d.renderer.state.TextureState;
import com.ardor3d.scenegraph.extension.BillboardNode;
import com.ardor3d.scenegraph.shape.Quad;
import com.ardor3d.ui.text.BMText;
import com.ardor3d.ui.text.BMText.AutoFade;
import com.ardor3d.ui.text.BMText.AutoScale;
import com.ardor3d.util.TextureManager;
import com.roslan.games.moo3d.utils.BMFontLoader;

/**
 * Implements the billboard for a star.
 * 
 * @author Roslan Amir
 * @version 1.0 - Aug 11, 2011
 */
public class StarBannerBillboard extends BillboardNode {

	/**
	 * The billboard background image
	 */
	private static final String IMAGE_BILLBOARD = "images/billboard_star_explored.png";

	/**
	 * The banner images
	 */
	private static final String[] IMAGES_BANNER = { 
		"images/banner_01_red.png", 
		"images/banner_02_yellow.png", 
		"images/banner_03_green.png",
		"images/banner_04_grey.png", 
		"images/banner_05_blue.png", 
		"images/banner_06_brown.png", 
		"images/banner_07_purple.png",
		"images/banner_08_orange.png" };

	/**
	 * The index into the array (indicating the color)
	 */
	private int bannerIndex;

	/**
	 * Constructor.
	 *
	 * @param name - the name of the star
	 */
	public StarBannerBillboard(String name) {
		super("StarBannerBillboardNode");
		this.setAlignment(BillboardAlignment.ScreenAligned);

		/*
		 * Sets the material state for this billboard node.
		 */
		final MaterialState ms = new MaterialState();
		ms.setEnabled(true);
		ms.setEmissive(ColorRGBA.WHITE);
		// ms.setAmbient(ColorRGBA.WHITE);
		// ms.setDiffuse(ColorRGBA.WHITE);
		// ms.setSpecular(ColorRGBA.WHITE);
		this.setRenderState(ms);

		final Quad quad = new Quad("StarBannerQuad", 1.70, 0.51); // image size: 340 x 102
		final TextureState ts = new TextureState();
		final Texture texture = TextureManager.load(IMAGE_BILLBOARD, Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessCompressedFormat, true);
		ts.setTexture(texture);
		ts.setEnabled(true);
		quad.setRenderState(ts);
		this.attachChild(quad);

		/*
		 * Add the text representing the star name.
		 */
		final BMText text = new BMText(name, name, BMFontLoader.getDefaultFont(), BMText.Align.East, BMText.Justify.Center);
		text.setFontScale(0.95);
		text.setSolidColor(ColorRGBA.BLACK_NO_ALPHA);
//		text.setDefaultColor(ColorRGBA.BLACK_NO_ALPHA);
//		text.setAutoFadeDistanceRange(nearOpaque, farTransparent);
		text.setAutoFade(AutoFade.FixedPixelSize);
		text.setAutoScale(AutoScale.CapScreenSize);
		text.setAutoRotate(true);
		text.setTranslation(0, 0, 0);
		this.attachChild(text);
	}

	/**
	 * Set the new banner for this Billboard instance.
	 * 
	 * @param index - the new banner index
	 */
	public void setBanner(int index) {
		if (index == this.bannerIndex)
			return;
		this.bannerIndex = index;
		TextureState ts = (TextureState) this.getLocalRenderState(StateType.Texture);
		ts.setTexture(TextureManager.load(IMAGES_BANNER[index], Texture.MinificationFilter.Trilinear, TextureStoreFormat.GuessCompressedFormat, true));
	}

	/**
	 * Set the new name for this star.
	 * 
	 * @param name - the new star name
	 */
	public void setStarName(String name) {
		
	}
}
